package com.hcbd.tdwt.ui.setting.xingyu.activity;

import android.annotation.SuppressLint;
import android.util.Log;
import android.widget.Switch;


import com.hcbd.tdwt.R;
import com.hcbd.tdwt.base.BaseActivity;
import com.hcbd.tdwt.base.Constant;
import com.hcbd.tdwt.util.SharedPreferencesUtil;


import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.CheckedChange;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

@SuppressLint("NonConstantResourceId")
@EActivity(R.layout.activity_notificat_setting)
public class NotificationSetActivity extends BaseActivity {
    @ViewById
    Switch switchBgNotification,unreadMsgNotSw,switchSound,switchShock;

    private SharedPreferencesUtil sp;
    @AfterViews
    protected void init() {
        sp=new SharedPreferencesUtil(this);
        if(Constant.Y.equals(sp.getAttribute(Constant.SWITCH_BG_NOTIFICATION))){
            switchBgNotification.setChecked(true);
        }else{
            switchBgNotification.setChecked(false);
        }
        if(Constant.Y.equals(sp.getAttribute(Constant.SWITCH_UNREAD_MSG))){
            unreadMsgNotSw.setChecked(true);
        }else{
            unreadMsgNotSw.setChecked(false);
        }
        if(Constant.Y.equals(sp.getAttribute(Constant.SWITCH_SOUND))){
            switchSound.setChecked(true);
        }else{
            switchSound.setChecked(false);
        }
        if(Constant.Y.equals(sp.getAttribute(Constant.SWITCH_SHOCK))){
            switchShock.setChecked(true);
        }else{
            switchShock.setChecked(false);
        }
    }
    @CheckedChange
    void switchBgNotification(boolean isOpen){
        if(isOpen){
            sp.addAttribute(Constant.SWITCH_BG_NOTIFICATION,Constant.Y);
        }else{
            sp.addAttribute(Constant.SWITCH_BG_NOTIFICATION,Constant.N);
        }
    }
    @CheckedChange
    void unreadMsgNotSw(boolean isOpen){
        if(isOpen){
            sp.addAttribute(Constant.SWITCH_UNREAD_MSG,Constant.Y);
        }else{
            sp.addAttribute(Constant.SWITCH_UNREAD_MSG,Constant.N);
        }
    }
    @CheckedChange
    void switchSound(boolean isOpen){
        if(isOpen){
            sp.addAttribute(Constant.SWITCH_SOUND,Constant.Y);
        }else{
            sp.addAttribute(Constant.SWITCH_SOUND,Constant.N);
        }
    }
    @CheckedChange
    void switchShock(boolean isOpen){
        if(isOpen){
            sp.addAttribute(Constant.SWITCH_SHOCK,Constant.Y);
        }else{
            sp.addAttribute(Constant.SWITCH_SHOCK,Constant.N);
        }
    }
    public void saveConfig(){
        Log.e("TAG", "保存配置" );
        Constant.bg_not_isOpen=sp.getAttribute(Constant.SWITCH_BG_NOTIFICATION);
        Constant.sound_isOpen=sp.getAttribute(Constant.SWITCH_SOUND);
        Constant.shock_isOpen=sp.getAttribute(Constant.SWITCH_SHOCK);
        Constant.unread_msg_isOpen=sp.getAttribute(Constant.SWITCH_UNREAD_MSG);
    }

    @Click
    void headLeft(){
        finish();
    }

    @Override
    protected void onDestroy() {
        saveConfig();
        super.onDestroy();
    }
}
